<template>
  <div class="box orderBox">
    <van-nav-bar
      title="订单"
      right-text="我的"
      left-arrow
      @click-left="$router.back()"
      @click-right="$router.push('/my')"
    />
    <van-tabs>
      <van-tab title="待支付" >
        <div class="order" v-for="item in toBePaid" :key="item.orderId">
          <h2 @click="$router.push('/orderDetail/'+item.orderId)">订单编号：{{item.time}}</h2>
          <van-card
            v-for="pro in item.proData"
            :key="pro.proId"
            :num="pro.num"
            :price="pro.price"
            :title="pro.proName"
            :thumb="pro.proImg"
          />
          <p><span>当前订单总价：{{allPrice(item)}}</span><button @click='goPay(item.orderId,2)' >去支付</button></p>
        </div>
      </van-tab>
      <van-tab title="待发货" >
        <div class="order" v-for="item in toBeDeliver" :key="item.orderId">
          <h2 @click="$router.push('/orderDetail/'+item.orderId)">订单编号：{{item.time}}</h2>
          <van-card
            v-for="pro in item.proData"
            :key="pro.proId"
            :num="pro.num"
            :price="pro.price"
            :title="pro.proName"
            :thumb="pro.proImg"
          />
          <p><span>当前订单总价：{{allPrice(item)}}</span></p>
        </div>
      </van-tab>
      <van-tab title="待收货" >
      </van-tab>
      <van-tab title="待评价" >
      </van-tab>
      <van-tab title="已完成" >
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import Vue from 'vue'
import { NavBar, Tab, Tabs, Card } from 'vant'
import { getOrder, setOrder } from '@/request/index'

Vue.use(NavBar)
Vue.use(Tab)
Vue.use(Tabs)
Vue.use(Card)

export default {
  data () {
    return {
      toBePaid: [],
      toBeDeliver: [],
      toBeReceiving: [],
      toBeEvaluate: [],
      finish: []
    }
  },
  mounted () {
    getOrder().then(data => {
      data.data.data.forEach(val => {
        switch (val.state) {
          case 1:
            this.toBePaid.push(val)
            break
          case 2:
            this.toBeDeliver.push(val)
            break
          case 3:
            this.toBeReceiving.push(val)
            break
          case 4:
            this.toBeEvaluate.push(val)
            break
          case 5:
            this.finish.push(val)
            break
        }
      })
    })
  },
  methods: {
    allPrice (data) {
      return data.proData.reduce((prev, now) => {
        return prev + now.price * now.num
      }, 0)
    },
    goPay (id, s) {
      // console.log('正在支付')
      // 修改状态
      setOrder({
        orderId: id,
        state: s
      }).then(data => {
        console.log(data)
      })
    }
  }
}
</script>

<style lang="scss">
.orderBox{
  .van-tabs{
    display: flex;
    flex-direction: column;
    height: 100%;
    .van-tabs__content{
      flex: 1;
      overflow: auto;
    }
  }
  .order{
    margin: 0.2rem 0;
    h2{
      font-size: 18px;
      padding: 0 0.1rem;
    }
    p{
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
